package net.sourceforge.m2settings.gui;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import net.sourceforge.m2settings.gui.listerners.ButtonCreateActionListener;
import net.sourceforge.m2settings.gui.listerners.ButtonRemoveActionListener;
import net.sourceforge.m2settings.model.ExtendableTableModel;

import org.apache.log4j.Logger;

public class JTablePanel extends JPanel {

	private static final long serialVersionUID = 1L;

	private JPanel jPanelButtons = null;

	private JScrollPane jScrollPane = null;

	private JTable jTable = null;

	private JButton jButtonCreate = null;

	private JButton jButtonRemove = null;

	private ExtendableTableModel tableModel = null;

	private Logger log = Logger.getLogger(JTablePanel.class); // @jve:decl-index=0:

	/**
	 * This is the default constructor
	 * 
	 * @param userSettings
	 * @param dataTableModel
	 * @param name
	 */
	public JTablePanel(ExtendableTableModel dataTableModel) {
		super();
		log.debug("Creating Panel " + dataTableModel.getElementName() + " with data " + dataTableModel);
		tableModel = dataTableModel;

		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setLayout(new BorderLayout());
		this.setSize(300, 200);
		this.add(getJScrollPane(), BorderLayout.CENTER);
		this.add(getJPanelButtons(), BorderLayout.SOUTH);
	}

	/**
	 * This method initializes jPanelButtons
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanelButtons() {
		if (jPanelButtons == null) {
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.BOTH;
			gridBagConstraints.gridy = -1;
			gridBagConstraints.weightx = 1.0;
			gridBagConstraints.weighty = 1.0;
			gridBagConstraints.gridx = -1;
			jPanelButtons = new JPanel();
			jPanelButtons.setLayout(new GridBagLayout());
			jPanelButtons.add(getJButtonCreate(), new GridBagConstraints());
			jPanelButtons.add(getJButtonRemove(), new GridBagConstraints());
		}
		return jPanelButtons;
	}

	/**
	 * This method initializes jScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getJScrollPane() {
		if (jScrollPane == null) {
			jScrollPane = new JScrollPane();
			jScrollPane.setViewportView(getJTable());
		}
		return jScrollPane;
	}

	/**
	 * This method initializes jTable
	 * 
	 * @return javax.swing.JTable
	 */
	private JTable getJTable() {
		if (jTable == null) {
			jTable = new JTable();
			jTable.setModel(tableModel);
		}
		return jTable;
	}

	/**
	 * This method initializes jButtonCreate
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonCreate() {
		if (jButtonCreate == null) {
			jButtonCreate = new JButton();
			jButtonCreate.setText("Create");
			jButtonCreate.setToolTipText("Add a new " + tableModel.getElementName());
			jButtonCreate.addActionListener(new ButtonCreateActionListener(
					getTableModel()));
		}
		return jButtonCreate;
	}

	/**
	 * This method initializes jButtonRemove
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJButtonRemove() {
		if (jButtonRemove == null) {
			jButtonRemove = new JButton();
			jButtonRemove.setText("Remove");
			jButtonRemove.setToolTipText("Delete the selected " + tableModel.getElementName());
			jButtonRemove.addActionListener(new ButtonRemoveActionListener(
					getTableModel(), jTable));
		}
		return jButtonRemove;
	}

	public ExtendableTableModel getTableModel() {
		return tableModel;
	}
}
